<?php

namespace app\api\controller;

use app\api\annotation\LoginRequired;
use app\api\basic\BaseController;
use app\api\logic\CollectionLogic;

use app\supplyDemand\logic\supplyDemandLogic;
use support\Request;


class CollectionController extends BaseController {
    protected $userInfo;

    /**
     * 当前登陆管理员ID
     */
    protected $userId;

    /**
     * 当前登陆管理员账号
     */
    protected $userName;
    public function __construct()
    {   
        parent::__construct();
        $this->userId=request()->userId;
        $this->userName=request()->userName;
        $this->userInfo=request()->userInfo;
    }

    public function saveCollection(Request $request){
        $param = $request->more([
           ['type',''],
           ['collection_id','']
        ]);
      
        $collection=new  CollectionLogic();
        $data=$collection->search(
            [
                'type'=>$param['type'],
                'collection_id'=>$param['collection_id'],
                'user_id'=>$this->userId
            ])
        
        ->find();
        $userId =empty($this->userId)?65:$this->userId;
        $ebSupplyDemand=new supplyDemandLogic();
        $product =$ebSupplyDemand->where(['sd_id'=>$param['collection_id']])->find()->toArray();
        if(!empty($data)){
           
            $collection->destroy($data->id,true);
            $result = supplyLog($userId, $product, 4);
            return $this->success('取消收藏成功');
        }
        $res=$collection->create([
            'type'=>$param['type'],
            'collection_id'=>$param['collection_id'],
            'user_id'=>$this->userId
        ]);
        if($param['type'] ==2){
                $result = supplyLog($userId, $product, 2);
        }
        if($res) return $this->success('收藏成功');
        return $this->fail('收藏失败');
    }

    //点击对接

    public function  dock(Request $request){
        $param = $request->more([
            ['sd_id','']
        ]);

        $ebSupplyDemand=new supplyDemandLogic();
        $product =$ebSupplyDemand->where(['sd_id'=>$param['sd_id']])->find()->toArray();
        //记录日志
        $userId =empty($this->userId)?65:$this->userId;  //测试----65
        $result =supplyLog($userId,$product, 3);

        return $this->success('成功');
    }
    
}